package com.web.base.user.dao;

import org.springframework.stereotype.Repository;

import com.blue.core.dict.Result;
import com.blue.jdbc.BaseDao;
import com.web.base.user.model.UserLoginLog;

/**
 * 登录日志数据库访问
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2017年04月16日
 */
@Repository
public class UserLoginLogDao extends BaseDao<UserLoginLog>
{
	public UserLoginLogDao()
	{
	}
	
	@Override
	protected void query(StringBuilder sql, UserLoginLog param)
	{
		if (param != null)
		{
			if (param.getUserId() != null && param.getUserId() > 0)
				sql.append(" and a.user_id=:userId");
				
			if (param.getResult() != null)
				sql.append(" and a.result=:result");
				
		}
		sql.append(" order by a.id desc");
	}

	@Override
	protected StringBuilder select()
	{
		StringBuilder sql = new StringBuilder(64);
		sql.append("select a.*, p.name app_name from uc_user_login_log a");
		sql.append(" left join auth_app p on p.id=a.app_id");
		sql.append(" where 1=1");
		return sql;
	}

	public void add(Integer appId, Integer userId, String descp, Result result, String ip)
	{
		UserLoginLog log = new UserLoginLog();
		log.setAppId(appId);
		log.setUserId(userId);
		log.setDescp(descp);
		log.setResult(result);
		log.setIp(ip);
		this.save(log);
	}
	
}
